package com.mkj.basic;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

@SuppressWarnings("all")
/**
 * @Author Arrebol
 * @Date 2024/8/5 19:51
 * @Project graphical programming
 * @Description：
 * 展示swing组件库基本组件
 */
public class SwingComponentDemo1 {

    //声明JFrame画布对象
    private final JFrame frame = new JFrame("测试swing基本组件");
    //声明菜单相关组件
    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("文件");
    JMenu editMenu = new JMenu("编辑");
    JMenu pattern = new JMenu("格式");
    JMenuItem comment = new JMenuItem("注释");
    JMenuItem cancelComment = new JMenuItem("取消注释");
    JMenuItem autoNewLine = new JMenuItem("自动换行");
    JMenuItem copy = new JMenuItem("复制",new ImageIcon("swing\\image\\component\\copy.png"));
    JMenuItem paste = new JMenuItem("粘贴",new ImageIcon("swing\\image\\component\\paste.png"));
    //声明文本域
    JTextArea textArea = new JTextArea(8, 20);
    //声明列表框（支持泛型）
    String[] colors = {"红色","绿色","蓝色"};
    JList<String> colorList = new JList<>(colors);
    //声明选择相关组件
    JComboBox<String> colorSelect = new JComboBox<>();
    //声明单选框
    JRadioButton male = new JRadioButton("男",false);
    JRadioButton female = new JRadioButton("女",true);
    //声明按钮组实现单选
    ButtonGroup genderButtonGroup=new ButtonGroup();
    //声明复选框
    JCheckBox isMarried = new JCheckBox("是否已婚？",true);
    //声明文本框
    JTextField textField = new JTextField(40);
    //声明按钮
    JButton okButton = new JButton("确定",new ImageIcon("swing\\image\\component\\ok.png"));
    //声明右键菜单
    JPopupMenu popupMenu = new JPopupMenu();
    //声明菜单项（只支持单选）
    JRadioButtonMenuItem metalItem = new JRadioButtonMenuItem("Metal 风格");
    JRadioButtonMenuItem nimbusItem = new JRadioButtonMenuItem("Nimbus 风格");
    JRadioButtonMenuItem windowsItem = new JRadioButtonMenuItem("Windows 风格");
    JRadioButtonMenuItem classicWindowsItem = new JRadioButtonMenuItem("Windows 经典风格",true);
    JRadioButtonMenuItem motifItem = new JRadioButtonMenuItem("Motif 风格");
    //声明按钮组
    ButtonGroup popupButtonGroup = new ButtonGroup();
    public void init() {
        //组装视图
        //组装底部
        JPanel bottomPanel = new JPanel();
        bottomPanel.add(textField);
        bottomPanel.add(okButton);
        frame.add(bottomPanel, BorderLayout.SOUTH);
        //组装选择相关组件
        JPanel selectPanel = new JPanel();
        colorSelect.addItem("红色");
        colorSelect.addItem("绿色");
        colorSelect.addItem("蓝色");
        selectPanel.add(colorSelect);
        genderButtonGroup.add(male);
        genderButtonGroup.add(female);
        selectPanel.add(male);
        selectPanel.add(female);
        selectPanel.add(isMarried);
        //组装文本域和选择相关组件
        Box topLeft = Box.createVerticalBox();
        topLeft.add(textArea);
        topLeft.add(selectPanel);
        //组装顶部
        Box top = Box.createHorizontalBox();
        top.add(topLeft);
        top.add(colorList);
        frame.add(top);
        //组装顶部菜单
        pattern.add(comment);
        pattern.add(cancelComment);
        editMenu.add(autoNewLine);
        //添加分割线
        editMenu.addSeparator();
        editMenu.add(copy);
        editMenu.add(paste);
        editMenu.addSeparator();
        editMenu.add(pattern);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        frame.setJMenuBar(menuBar);
        //组装右键菜单
        popupButtonGroup.add(metalItem);
        popupButtonGroup.add(nimbusItem);
        popupButtonGroup.add(windowsItem);
        popupButtonGroup.add(classicWindowsItem);
        popupButtonGroup.add(motifItem);
        //创建事件监听器
        ActionListener listener = e -> {
            //当前选择的是哪个风格
            try {
                changeFlavor(e.getActionCommand());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        };
        metalItem.addActionListener(listener);
        nimbusItem.addActionListener(listener);
        windowsItem.addActionListener(listener);
        classicWindowsItem.addActionListener(listener);
        motifItem.addActionListener(listener);
        popupMenu.add(metalItem);
        popupMenu.add(nimbusItem);
        popupMenu.add(windowsItem);
        popupMenu.add(classicWindowsItem);
        popupMenu.add(motifItem);
        //不需要监听鼠标事件
        textArea.setComponentPopupMenu(popupMenu);
        //不需要为X号绑定鼠标点击事件
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    //定义一个方法，用于改变界面风格
    private void changeFlavor(String command) throws Exception {
        switch (command) {
            case "Metal 风格":
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                break;
            case "Nimbus 风格":
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                break;
            case "Windows 风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                break;
            case "Windows 经典风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                break;
            case "Motif 风格":
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                break;
        }
        //刷新组件外观
        SwingUtilities.updateComponentTreeUI(frame.getContentPane());
        SwingUtilities.updateComponentTreeUI(menuBar);
        SwingUtilities.updateComponentTreeUI(popupMenu);
    }
    public static void main(String[] args) {
        new SwingComponentDemo1().init();
    }
}
